﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NotAClue
{
    public static class DateExtensionMethods
    {
        /// <summary>
        /// Ins the last quarter.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <param name="currentDate">The current date.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static Boolean InLastQuater(this DateTime date, DateTime currentDate)
        {
            var soq = currentDate.FirstDayOfQuarter();
            var eoq = currentDate.LastDayOfQuarter();
            return (date >= soq && date <= eoq);
        }

        /// <summary>
        /// Gets the quarter from the date supplied.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static int GetQuarter(this DateTime date)
        {
            return (date.Month - 1) / 3 + 1;
        }

        /// <summary>
        /// Firsts the day of year.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        public static DateTime FirstDayOfYear(this DateTime date)
        {
            return new DateTime(DateTime.Today.Year, 1, 1);
        }

        /// <summary>
        /// Lasts the day of Q year.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        public static DateTime LastDayOfQYear(this DateTime date)
        {
            return new DateTime(DateTime.Today.Year, 12, 31);
        }

        /// <summary>
        /// Returns first the day of quarter with quarters
        /// starting at the beginning of Jan/Apr/Jul/Oct
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static DateTime FirstDayOfQuarter(this DateTime date)
        {
            var quater = date.GetQuarter();
            var month = 3 * quater - 2;
            return new DateTime(date.Year, month, 1);
        }

        /// <summary>
        /// Returns the last the day of quarter with 
        /// quarters ending at the end of Mar/Jun/Sep/Dec
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static DateTime LastDayOfQuarter(this DateTime date)
        {
            var quarter = date.GetQuarter();
            var month = 3 * quarter;
            return new DateTime(date.Year, month, DateTime.DaysInMonth(date.Year, month));
        }

        /// <summary>
        /// Gets last day of the week.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns>Last day of the wee for the given date.</returns>
        /// <remarks></remarks>
        public static DateTime LastDayOfWeek(this DateTime date)
        {
            var dayOfWeek = (int)DateTime.Today.DayOfWeek + 1;
            return date.AddDays(dayOfWeek - 7);
        }

        /// <summary>
        /// Firsts the day of week.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        public static DateTime FirstDayOfWeek(this DateTime date)
        {
            var dayOfWeek = (int)DateTime.Today.DayOfWeek;
            return date.AddDays(-(dayOfWeek));
        }
    }
}
